package com.example.back.controller;

import com.example.back.model.Boardgame;
import com.example.back.model.Scenario;
import com.example.back.service.BoardgameService;
import com.example.back.service.ScenarioService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 剧本杀控制层
 */
@RestController
@RequestMapping("/scenario")
public class ScenarioController {
    private static final Logger logger = LogManager.getLogger(ScenarioController.class);

    @Resource
    private ScenarioService scenarioService;

    @RequestMapping("/getScenarioList")
    @ResponseBody
    public List<Scenario> getScenarioList(){
        return scenarioService.findAllScenario();
    }

    @RequestMapping("/insertScenario")
    public String insertScenario(String name, String introduction, int lowerLimit, int upperLimit) {
        String rs = "";
        if (scenarioService.insertScenario(name, introduction, lowerLimit, upperLimit, 0)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新剧情杀添加成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新剧情杀添加失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updateScenario")
    public String updateScenario(int id, int position) {
        String rs = "";
        if (position == 0) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>输入的位置信息非法，请重新输入！点击返回（3秒后自动返回）</button>";
        } else if (scenarioService.updateScenario(id, position)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>剧情杀位置信息更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>剧情杀位置信息更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/deleteScenario")
    public String deleteScenario(int id) {
        String rs = "";
        if (scenarioService.deleteScenario(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>剧情杀删除成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>剧情杀删除失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("watchKPhoto")
    public String watchKPhoto(int id) {
        return "<img src='" + scenarioService.findPhotoByID(id) + "'>";
    }
}
